<template>
	<view class="search">
		<!-- #ifdef MP-WEIXIN -->
		<uni-search-bar @confirm="search"  clearButton="auto" @input='inp'></uni-search-bar>
		<!-- #endif -->
		<Line></Line>
		<view class="search-item">
			<view class="search-title">
				<view>最近搜索</view>
				<view class="iconfont icon-del" @tap="clearHistory"></view>
			</view>
			<view class="search-names" v-if="searchData.length>0">
				<view class="search-name" v-for="(item,index) in searchData" :key="index" @tap="toSearchList(item)">{{item}}</view>
		
			</view>
			<view v-else class="search-end">暂无搜索记录</view>
		</view>


		<view class="search-item">
			<view>
				<view>热门搜索</view>
			</view>
			<view class="search-names">
				<view class="search-name">鸡蛋</view>
				<view class="search-name">土猪肉</view>
				<view class="search-name">东北大米</view>
				<view class="search-name">青菜</view>
				<view class="search-name">面粉</view>
			</view>
		</view>
	</view>

</template>

<script>
	import Line from '@/components/common/Line.vue'
	export default {
		data() {
			return {
				keyword: '',
				//搜索过的记录
				searchData:[]
			}
		},
		components: {
			Line
		},
		//页面加载的时候
		onLoad() {
			uni.getStorage({
				key:'searchData',
				success: (res) => {
				this.searchData=JSON.parse(res.data)	
				console.log(this.searchData,"sousuo");
				}
			})
		},
		onNavigationBarButtonTap(e) {
			this.search()
		
		},
		
		// 	监听原生标题栏搜索输入框输入内容变化事件（APP）
		onNavigationBarSearchInputChanged(e) {
			this.keyword = e.text
		},
			//监听软键盘回车搜索
		onNavigationBarSearchInputConfirmed() {
				this.search()
},
		methods: {

			//监听input组件搜索框内容的值变换
			inp(e) {
				this.keyword = e
			},
			//判断关键词是否为空和跳转页面
			search(){
				
				if(this.keyword===""){
					return uni.showToast({
						title:"输入框不能为空",
						icon:"none"
					})
				}else{
					uni.navigateTo({
						
					// url: "../search-list/search-list?keyword="+this.keyword+""
					 url: `../search-list/search-list?keyword=${this.keyword}`
				})
				}
				this.addSearch()
				
			},
			//清除搜索记录
			clearHistory(){
				uni.showModal({
					title:'重要提示',
					content:'是否要清除搜索记录',
					cancelText:'取消',
					confirmText:"确定",
					success: (res) => {
						if(res.confirm){
							uni.removeStorage({
								key:"searchData"
							})
							//更新视图层
							this.searchData=[]
						}
					}
				
				})
			},
			//最近搜索词
			addSearch(){
				let idx =this.searchData.indexOf(this.keyword)
		if(idx<0){
			this.searchData.unshift(this.keyword);
		}else{
			this.searchData.unshift(this.searchData.splice(idx,1)[0]);
		}
		console.log(this.searchData);
		//本地存储数据
		uni.setStorage({
			key:'searchData',
			data:JSON.stringify(this.searchData)
		})
		
			},
			//点击搜索记录进入页面
			toSearchList(item){
				uni.navigateTo({
					url:`../search-list/search-list?keyword=${item}`
				})
			}
		}
	}
	
</script>

<style scoped>
	.search-item {
		padding: 20rpx;
	}

	.search-title {
		color: #636263;
		display: flex;
		justify-content: space-between
	}

	.search-name {
		font-size: 14px;
		padding: 4rpx 24rpx;
		background-color: #E1E1E1;
		display: inline-block;
		color: #636263;
		border-radius: 26rpx;
		margin: 10rpx;
	}

	.search-names {
		padding: 8rpx;
	}
	.search-end{
		text-align: center;
	}
</style>
